from typing import *


class Solution:
    def remainingMethods(
        self, n: int, k: int, invocations: List[List[int]]
    ) -> List[int]:
        nexts = DefaultDict(list)
        ins = [0] * n
        for a, b in invocations:
            nexts[a].append(b)
            ins[b] += 1
        suspicious = [False] * n
        suspicious[k] = True

        def dfs(p):
            if p not in nexts:
                return
            for v in nexts[p]:
                if not suspicious[v]:
                    suspicious[v] = True
                    dfs(v)

        dfs(k)

        for i in range(n):
            if suspicious[i]:
                for v in nexts[i]:
                    ins[v] -= 1
        for i in range(n):
            if suspicious[i] and ins[i] > 0:
                return list(range(n))
        ans = []
        for i in range(n):
            if not suspicious[i]:
                ans.append(i)
        return ans
